import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:hotpot/widgets/widget_ext.dart';

import 'z_container.dart';

class PopupBtn<T> extends StatelessWidget {
  final String? title;
  final ValueChanged<T>? onChanged;
  final String? hintText;
  final List<T>? list;
  final String Function(T data) getText;
  final String? text;

  const PopupBtn({
    super.key,
    this.title,
    this.onChanged,
    this.hintText,
    this.list,
    this.text,
    required this.getText,
  });

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        title.text(size: 7, color: Colors.white, bold: true),
        4.hSizedBox,
        ZContainer(
          height: 25,
          width: double.infinity,
          radius: 5,
          color: const Color(0xFF1F1D2B),
          side: const BorderSide(color: Color(0xFF393C49), width: 1),
          padding: EdgeInsets.symmetric(horizontal: 12.r),
          child: Stack(
            children: [
              const Icon(Icons.arrow_drop_down_outlined, color: Colors.white).positioned(right: 0, top: 0, bottom: 0),
              PopupMenuButton<T>(
                itemBuilder: _item,
                onOpened: () => FocusScope.of(context).requestFocus(FocusNode()),
                child: text.text(color: Colors.white, size: 6).alignLeft(),
                onSelected: (data) {
                  onChanged?.call(data);
                },
              ).positioned(left: 0, right: 0, top: 0, bottom: 0),
            ],
          ),
        ),
      ],
    );
  }

  List<PopupMenuEntry<T>> _item(BuildContext context) {
    if (list == null) return [];
    return list!.map((e) => PopupMenuItem<T>(value: e, child: getText(e).text())).toList();
  }
}
